package com.li.o2oSystem.util;

import javax.servlet.http.HttpServletRequest;
/**
 * 校验验证码工具类
 * @author Administrator
 */
public class CodeUtil {
	public static boolean checkVerifyCode(HttpServletRequest request){
		StringBuffer sb1 = new StringBuffer();
		StringBuffer sb2 = new StringBuffer();
		//获取实际验证码
		String verifyCodeExpected = (String) request.getSession().getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
		String verifyCodeActual = HttpServletRequestUtil.getString(request, "verifyCodeActual");
		for(int i = 0;i<verifyCodeExpected.length();i++){//处理自动生成的验证码
			char c = verifyCodeExpected.charAt(i);
			if(Character.isUpperCase(c)){//如果字符是大写
				sb1.append(Character.toLowerCase(c));
			}else if(Character.isDigit(c)){
				sb1.append(c);
			}else{
				sb1.append(c);
			}
		}
		for(int i = 0;i<verifyCodeActual.length();i++){//处理输入的验证码
			char c = verifyCodeActual.charAt(i);
			if(Character.isUpperCase(c)){//如果字符是大写
				sb2.append(Character.toLowerCase(c));
			}else if(Character.isDigit(c)){
				sb2.append(c);
			}else{
				sb2.append(c);
			}
		}
		String strExpected = sb1.toString();
		String strActual = sb2.toString();
		if(strActual == null || !strActual.equals(strExpected)){//对比验证码，如果不相等返回false
			return false;
		}
		return true;
	}
}
